package com.lanou.security;

import com.lanou.mapper.LoginMapper;
import com.lanou.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.List;

public class SpringSecurityUserDetailsService implements UserDetailsService {
@Autowired
private LoginMapper loginMapper;
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Override
public UserDetails loadUserByUsername(String s) throws
        UsernameNotFoundException {
System.out.println(bCryptPasswordEncoder.encode("admin123"));
SysUser sysUser = loginMapper.login(s);
if (sysUser == null){
return null;
}
List<GrantedAuthority> list = new ArrayList<>();
User user = new User(sysUser.getUserName(),
sysUser.getPassword(), list);
return user;
}
}